<?php
if (!defined('IN_D')) die("Error 404");
function box_main_menu($file_tpl = 'main_menu') {
	global $tpl;
	return $tpl->get_tpl($file_tpl);
}
function box_category_menu($type="media") {
	global $DB,$conf,$tpl,$func;
	$html='';
	$file_tpl = 'box_category_menu';
	$main = $tpl->get_tpl($file_tpl);
	$t['parent'] = $tpl->get_block_from_str($main,'cat_list.parent',1);
	$t['sub'] = $tpl->get_block_from_str($main,'cat_list.sub',1);
	if($type=="media")
	 $q = $DB->query("SELECT cat_id, cat_name,cat_name_ascii FROM ".$conf['prefix']."cat WHERE cat_type=1 AND (sub_id IS NULL OR sub_id = 0) ORDER BY cat_order ASC");
 	elseif($type=="movie")
	 $q = $DB->query("SELECT cat_id, cat_name,cat_name_ascii FROM ".$conf['prefix']."cat WHERE cat_type=2 AND (sub_id IS NULL OR sub_id = 0) ORDER BY cat_order ASC");
	while ($r = $DB->fetch_row($q)) {
		$html .= $tpl->assign_vars($t['parent'],
			array(
				'cat_parent.URL' => $func->change_link('cat',$r['cat_id'],$func->title_2_ascii($r['cat_name_ascii'])),
				'cat_parent.NAME' => $func->unhtmlchars($r['cat_name']),
			)
		);
		if($type=="media")
		 $q2 = $DB->query("SELECT cat_id, cat_name,cat_name_ascii FROM ".$conf['prefix']."cat WHERE cat_type=1 AND sub_id = '".$r['cat_id']."' ORDER BY cat_order ASC");
		elseif($type=="movie")
		 $q2 = $DB->query("SELECT cat_id, cat_name,cat_name_ascii FROM ".$conf['prefix']."cat WHERE cat_type=2 AND sub_id = '".$r['cat_id']."' ORDER BY cat_order ASC");
		while ($r2 = $DB->fetch_row($q2)) {
			$html .= $tpl->assign_vars($t['sub'],
				array(
					'cat_sub.URL' => $func->change_link('cat',$r2['cat_id'],$func->title_2_ascii($r2['cat_name_ascii'])),
					'cat_sub.NAME' => $func->unhtmlchars($r2['cat_name']),
				)
			);
		}
	}
	$html = $tpl->assign_blocks_content($main,array(
		'cat_list'	=>	$html
		)
	);
	return $html;
}
function box_announcement_management($file_tpl = 'box_announcement_management') {
	global $DB, $tpl,$conf,$func;
	$html = $tpl->get_tpl($file_tpl);
	$contents = stripslashes($conf['announcement']);
	//$contents = $func->text_2_html($contents);
	if (!$contents) return '';
	$html = $tpl->assign_vars($html,
		array(
			'ann_manager.CONTENT'	=>	$contents,
		)
	);
	return $html;
}

function box_announcement($contents) {
	global $DB, $tpl,$conf,$func;
	$html = $tpl->get_tpl("announcement");
	$contents = stripslashes($contents);
	$contents = $func->text_2_html($contents);
	if (!$contents) return '';
	$html = $tpl->assign_vars($html,
		array(
			'ann.CONTENT'	=>	$contents,
		)
	);
	return $html;
}
function box_multimedia($type,$multi_type, $number = 10,$id='', $apr = 1, $page='',$def_id='') {
	global $DB,$conf,$tpl,$func,$lang;
	$html='';
	$page = (int)$page;
	if(!$page) $page = 1;
	if ($type == 'New') {
		$result = $DB->query("SELECT multi_id, multi_name, multi_name_ascii, multi_singer, multi_cat, multi_img FROM ".$conf['prefix']."multimedia WHERE multi_type={$multi_type} ORDER BY multi_id DESC LIMIT $number");
		if($multi_type==2)
		$block = 'box_show_multimedia';
		else $block = 'box_multimedia';
    }
	if ($type == 'Top') {
		$result = $DB->query("SELECT multi_id, multi_name,multi_name_ascii, multi_singer, multi_cat, multi_img FROM ".$conf['prefix']."multimedia WHERE  multi_viewed > 0 AND multi_type={$multi_type} ORDER BY multi_viewed DESC LIMIT $number");
		$block = 'box_multimedia';
	}
	if ($type == 'Top_Month') {
		$result = $DB->query("SELECT multi_id, multi_name,multi_name_ascii, multi_singer, multi_cat, multi_img FROM ".$conf['prefix']."multimedia WHERE multi_viewed_month > 0 AND multi_type={$multi_type} ORDER BY multi_viewed_month DESC LIMIT ".(($page-1)*$number).",".$number);
		if($multi_type==2)
		$block = 'box_show_multimedia';
		else $block = 'box_multimedia';
	}
	if ($type == 'Album_Cat' || $type=='Movie_In_Cat' || $type=='Album_In_Cat') {
		($multi_type==1)?$cat_type=1:$cat_type=2;
		$q_chk=$DB->query("SELECT sub_id,cat_type,cat_name,cat_name_ascii FROM ".$conf['prefix']."cat WHERE cat_id = '{$id}' AND cat_type={$cat_type}");
		$check = $DB->fetch_row($q_chk);
		if (!is_null($check['sub_id']) && $check['sub_id'] != 0) {
			if(!empty($def_id)){
				$result=$DB->query("SELECT * FROM {$conf['prefix']}multimedia WHERE multi_cat='{$id}' AND multi_type='{$multi_type}' AND multi_id<>'{$def_id}' ORDER BY multi_id DESC LIMIT ".(($page-1)*$number).",".$number);
				$al_tt='';
			}
			else{
				$result=$DB->query("SELECT * FROM {$conf['prefix']}multimedia WHERE multi_cat='{$id}' AND multi_type='{$multi_type}' ORDER BY multi_id DESC LIMIT ".(($page-1)*$number).",".$number);
				$al_tt=$DB->query("SELECT * FROM {$conf['prefix']}multimedia WHERE multi_cat='{$id}' AND multi_type='{$multi_type}' ORDER BY multi_id DESC");
			}
		}
		else{
				$list_q = $DB->query("SELECT cat_id FROM ".$conf['prefix']."cat WHERE sub_id = '{$id}' AND cat_type={$cat_type}");
				$in_sql = '';
				while ($list_ro = $DB->fetch_row($list_q)) $in_sql .= "'".$list_ro['cat_id']."',";
				$in_sql = substr($in_sql,0,-1);
				if (!$in_sql) $in_sql = -1;
			if(!empty($def_id)){
				$result=$DB->query("SELECT * FROM {$conf['prefix']}multimedia WHERE multi_cat IN ($in_sql) AND multi_type='{$multi_type}' AND multi_id<>'{$def_id}' ORDER BY multi_id DESC LIMIT ".(($page-1)*$number).",".$number);
				$al_tt='';
			}
			else{
				$result=$DB->query("SELECT * FROM {$conf['prefix']}multimedia WHERE multi_cat IN ($in_sql) AND multi_type='{$multi_type}' ORDER BY multi_id DESC LIMIT ".(($page-1)*$number).",".$number);
				$al_tt=$DB->query("SELECT * FROM {$conf['prefix']}multimedia WHERE multi_cat IN ($in_sql) AND multi_type='{$multi_type}' ORDER BY multi_id DESC");
			}
		}

		$block = 'box_show_multimedia';
	}
	if ($type == 'Album_Singer') {
		$result = $DB->query("SELECT multi_id, multi_name,multi_name_ascii, multi_cat, multi_img FROM ".$conf['prefix']."multimedia WHERE multi_singer={$id} AND multi_type={$multi_type} ORDER BY multi_id DESC LIMIT ".(($page-1)*$number).",".$number);
		$al_tt = $DB->query("SELECT multi_id, multi_name,multi_name_ascii, multi_singer, multi_cat, multi_img FROM ".$conf['prefix']."multimedia WHERE multi_singer={$id} AND multi_type={$multi_type} ORDER BY multi_id");
		$block = 'box_show_multimedia';
	}
	if ($type == 'Random') {
		$result = $DB->query("SELECT multi_id, multi_name, multi_name_ascii, multi_singer, multi_cat, multi_img FROM ".$conf['prefix']."multimedi WHERE multi_type={$multi_type} a ORDER BY RAND() LIMIT $number");
		$block = 'box_show_multimedia';
	}
	$main = $tpl->get_tpl($block);
	$t['link'] = $tpl->get_block_from_str($main,$block.'.row',1);
	$t['begin_tag'] = $tpl->get_block_from_str($main,$block.'.begin_tag',1);
	$t['end_tag'] = $tpl->get_block_from_str($main,$block.'.end_tag',1);
	if (!$DB->num_rows($result)) $html = $lang['updating'];
	if($multi_type==1) $multitype=$lang['album'];
	elseif($multi_type==2) $multitype=$lang['movie'];
	if(in_array($type,array('Album_Singer','Album_Cat'))){
		$total = $DB->num_rows($al_tt);
		$multi_total="{$lang['total']} <b>{$total}</b> {$multitype}</b>";
	}
	else{
		$total = $DB->num_rows($DB->query("SELECT multi_name FROM ".$conf['prefix']."multi WHERE multi_viewed > 0 AND multi_type={$multi_type}"));
		$multi_total="";
	}
	if(in_array($type,array('Album_In_Cat','Movie_In_Cat'))){
		$cat_ascii=$func->get_data("cat",$id,"cat_name_ascii");
		$page_block ="<center><a href='".$func->change_link("cat",$id,$func->title_2_ascii($cat_ascii))."'><font color=blue><b>{$lang['view_more']}</b></font></a></center>";
	}
	else
		$page_block = $func->view_pages_ajax($total,$number,$page,"change_box_multi_page('".$type."',{$number},",true,$id);
	$main = $tpl->assign_vars($main,
    	array(
			'multi.PAGES' => $page_block,
			'multi.TOTAL' => $multi_total,
		)
	);
	$i = 0;
	while ($r = $DB->fetch_row($result)) {
		$multi_img = $func->get_img('multimedia',$r['multi_id']);
		if($multi_type==1){
			$width=90;
			$height=90;
			$link1=$func->change_link('album',$r['multi_id'],$func->title_2_ascii($r['multi_name_ascii']));
			$sub_name=$func->get_data('singer',$r['multi_singer']);
			$link2=$func->change_link('singer',$r['multi_singer'],$func->title_2_ascii($sub_name,true));			
		}
		else{
			$width=150;
			$height=200;
			$link1=$func->change_link('movie',$r['multi_id'],$func->title_2_ascii($r['multi_name_ascii']));
			$sub_name=$func->get_data('cat',$r['multi_cat']);
			$link2=$func->change_link('cat',$r['multi_cat'],$func->title_2_ascii($sub_name,true));	
		}	
		if ($t['begin_tag'] && fmod($i,$apr) == 0) $html .= $t['begin_tag'];
		$html .= $tpl->assign_vars($t['link'],
			array(
				'multi.URL'		=>	$link1,
				'multi.NAME'	=>	$func->unhtmlchars($r['multi_name']),
				'multi.SHORT'	=>	$func->getwords($r['multi_name'],3),
				'multi.IMG'		=>	$multi_img,
				'WIDTH'			=> $width,
				'HEIGHT'		=> $height,
				'sub.URL'	=>	$link2,
				'sub.NAME'	=>	$sub_name,
			)
		);
		if ($t['end_tag'] && fmod($i,$apr) == $apr - 1) $html .= $t['end_tag'];
		$i++;
	}
	$main = $tpl->assign_vars($main,
     array(
			'top.MONTH' => $conf['current_month'],
		)
	);
	$main = $tpl->assign_blocks_content($main,array(
		$block => $html,
		)
	);
	return $tpl->parse_tpl($main);
} 


function box_top_media($type,$number = 10) {
	global $DB,$conf,$tpl,$func,$lang;
	if ($type == 'Top_Play_Month') {
		$result = $DB->query("SELECT m_id, m_title,m_title_ascii, m_singer, m_cat, m_type FROM ".$conf['prefix']."media WHERE  m_viewed_month > 0 AND (m_multimedia=0 OR m_multimedia IN (select multi_id from ".$conf['prefix']."multimedia where multi_type=1)) ORDER BY m_viewed_month DESC LIMIT ".$number);
		$block = 'box_media_month';
		$title_box=$lang['top_play_month'];
		$cls1="num";
		$cls2="num_2";
	}
	if ($type == 'Top_Download_Month') {
		$result = $DB->query("SELECT m_id, m_title, m_title_ascii, m_singer FROM ".$conf['prefix']."media WHERE m_downloaded_month >0 AND (m_multimedia=0 OR m_multimedia IN (select multi_id from ".$conf['prefix']."multimedia where multi_type=1)) ORDER BY m_downloaded_month DESC LIMIT ".$number);
		$block = 'box_media_month';
		$title_box=$lang['top_download_month'];
		$cls1="num_2";
		$cls2="num";
	}
	$main = $tpl->get_tpl($block);
	$html='';
	$t['link'] = $tpl->get_block_from_str($main,$block.'.row',1);
	if (!$DB->num_rows($result)) $html = "<tr><td>{$lang['updating']}</td></tr>";
	else{
		$i = 0;
		while ($r = $DB->fetch_row($result)) {
		
			 $class = (fmod($i,2) == 0)?$cls1:$cls2;
			$singer_name=$func->get_data('singer',$r['m_singer']);
			$html .= $tpl->assign_vars($t['link'],
				array(
					'song.CLASS'	=> $class,
					'song.NUMBER'	=> $n=$i+1,
					'song.URL'		=>	$func->change_link("song",$r['m_id'],$func->title_2_ascii($r['m_title_ascii'])),
					'song.TITLE'	=>	$func->unhtmlchars($r['m_title']),
					'song.SHORT'	=>	$func->getwords($r['m_title'],5),
					'song.URL_SINGER'	=>	$func->change_link("singer",$r['m_singer'],$func->title_2_ascii($singer_name,true)),
					'song.SINGER_NAME'	=>	$singer_name,
				)
			);
			$i++;
		}

	}
	$main = $tpl->assign_vars($main,
     array(
	 		'top.LANG'	=> $title_box,
			'top.MONTH' => $conf['current_month'],
		)
	);
	$main = $tpl->assign_blocks_content($main,array(
		$block => $html,
		)
	);
	return $tpl->parse_tpl($main);
} 
function box_singer_list($type, $file_tpl='', $page='', $number = 20) {
	global $DB,$conf,$tpl,$func,$lang;
	$page = (int)$page;
	$html='';
	if(!$page) $page = 1;
	$q = $DB->query("SELECT * FROM ".$conf['prefix']."singer WHERE singer_type = '".$type."' ORDER BY singer_name ASC LIMIT ".(($page-1)*$number).",".$number);
	//$q = $DB->query("SELECT * FROM ".$conf['prefix']."singer WHERE singer_type = '".$type."' ORDER BY RAND() LIMIT $number");
	//$q = $DB->query("SELECT * FROM ".$conf['prefix']."singer WHERE singer_type = '".$type."' ORDER BY singer_name ASC");
	switch ($type) {
		case 1 :
			if (!$file_tpl) $file_tpl = 'box_singer_vn';
			$block = 'vnsinger';
			$unknownID = -1;
			$url_list_singer=$func->change_link("singer","domestic",$func->title_2_ascii($lang['list_domestic_singer'],true));
		break;
		case 2 :
			if (!$file_tpl) $file_tpl = 'box_singer_fr';
			$block = 'frsinger';
			$unknownID = -2;
			$url_list_singer=$func->change_link("singer","foreign",$func->title_2_ascii($lang['list_foreign_singer'],true));
		break;

	}
	$main = $tpl->get_tpl($file_tpl);
	$t['link'] = $tpl->get_block_from_str($main,$block.'.row',1);
	if (!$DB->num_rows($q)) $html = "Ðang Cập Nhật";
	$total = $DB->num_rows($DB->query("SELECT singer_name FROM ".$conf['prefix']."singer WHERE singer_type IN('1')"));
	if($page == 1) $vl = 'false';
	else $vl = 'change_vnsinger('.($page-1).');';
	$first = '<a style="cursor:pointer" onclick="return '.$vl.'" title="Xem trang trước"><img src="images/prev.gif"></a>';
	if($page == ceil($total/$number)) $vl = 'false';
	else $vl = 'change_vnsinger('.($page+1).');';
	$last = '<a style="cursor:pointer" onclick="return '.$vl.'" title="Xem trang kế tiếp"><img src="images/next.gif"></a>';
	//$page_block = $func->view_pages_ajax($total,$number,$page,'change_vnsinger($m);');
	$main = $tpl->assign_vars($main,
    	array(
		    'singer.FIRST' => $first,
            'singer.LAST' => $last,
			//'singer.PAGE' => $page_block,
			'singer.URL_LIST'	=> $url_list_singer,
		)
	);
	$i = 0;
	while ($r = $DB->fetch_row($q)) {
		$html .= $tpl->assign_vars($t['link'],
			array(
				'singer.NAME' => $func->unhtmlchars($r['singer_name']),
				'singer.URL'	=>	$func->change_link('singer',$r['singer_id'],$func->title_2_ascii($r['singer_name_ascii'])),
			)
		);
	$i++;
	}
	$html = $tpl->assign_blocks_content($main,array(
			$block	=>	$html
		)
	);
	return $html;
}

function box_comment($multi_id='',$media_id='', $compose=false, $number='') {
	global $DB,$conf,$tpl,$func,$lang;
	if(!$number) $number=7;
	$block=	"box_comment";
	$ro=$tpl->get_tpl($block);
	$ro = $tpl->assign_vars($ro,array(
			'SHOW_COMPOSE' => $tpl->get_tpl("box_compose"),
			'cm.SCRIPT'	   => "<script>".($multi_id?"change_comment_page('multi',{$number},{$multi_id},1);":"change_comment_page('media',{$number},{$media_id},1);")."</script>",
			'multi.ID'	   => $multi_id,	
			'media.ID'	   => $media_id,
		)
	);
	
	return $ro;
}
function box_list_comment($multi_id='',$media_id='', $number='',$page='') {
	global $DB,$conf,$tpl,$func,$lang,$logged_in;
	if(!$number) $number=7;
	if(!$page) $page=1;
	$limit=($page-1)*$number;
		if($multi_id){ 
			if($multi_id=="movie"){
				$qr=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_multi<>'' ORDER BY cm_id DESC LIMIT {$limit},{$number}");
				$tt_cm=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_multi<>'' ");
			}
			else{
				$qr=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_multi={$multi_id} ORDER BY cm_id DESC LIMIT {$limit},{$number}");
				$tt_cm=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_multi={$multi_id}");
			}
		}
		elseif($media_id){
			if($media_id=="music"){
				$qr=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_media<>'' ORDER BY cm_id DESC LIMIT {$limit},{$number}");
				$tt_cm=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_media<>'' ");
			}else{
		 	$qr=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_media={$media_id} ORDER BY cm_id DESC LIMIT {$limit},{$number}");
			$tt_cm=$DB->query("SELECT * FROM {$conf['prefix']}comment WHERE cm_media={$media_id}");
			}
		}

	$block=	"box_list_comment";
	$ro=$tpl->get_tpl($block);
	$html='';
	$total=$DB->num_rows($tt_cm);
	$cm_total="";
	$view_pages="";

	if(($multi_id!="movie" && $media_id=="")||($multi_id=="" && $media_id!="music")){
		$cm_total=$lang['total']." <b>".$total."</b> ".$lang['comment'];
		$view_pages=$func->view_pages_ajax($total,$number,$page,($multi_id?"change_comment_page('multi',{$number},":"change_comment_page('media',{$number},"),true,($multi_id?$multi_id:$media_id));
	}
	$t['row'] = $tpl->get_block_from_str($ro,$block.'_row',1);
	$i = 0;
	
	while ($r = $DB->fetch_row($qr)) {
		$url_include='';
		$cm_content=$func->smiles_replace($func->unhtmlchars($r['cm_content']));
		if($multi_id=="movie" && empty($media_id)){
			$title_include=$func->get_data("multimedia",$r['cm_multi'],"multi_name");
			$title_short=$func->getwords($title_include,5);
			$link=$func->change_link("movie",$r['cm_multi'],$func->title_2_ascii($title_include,true));
			$url_include= "<a href='{$link}' title='{$title_include}'><font color=#00FFFF><b>{$title_short}</b></font></a><br>";
			$cm_content=$func->getwords($cm_content,17);
			
		}
		elseif($media_id=="music" && empty($multi_id)){
			$title_include=$func->get_data("song",$r['cm_media'],"m_title");
			$title_short=$func->getwords($title_include,5);
			$link=$func->change_link("song",$r['cm_media'],$func->title_2_ascii($title_include,true));
			$url_include= "<a href='{$link}' title='{$title_include}'><font color=#00FFFF><b>{$title_short}</b></font></a><br>";
			$cm_content=$func->getwords($cm_content,17);
		}
		else
			$avatar_include=($r['cm_user_id'])?("<img src='".$func->get_img('avatar',$r['cm_user_id'])."' width=75 height=75 border=0>"):"<img src='{TPL_LINK}/img/guest.gif' width=75 height=75 border=0>";
		$html .= $tpl->assign_vars($t['row'],
			array(
				'cm.URL_INCLUDE'	=>$url_include,	
				'cm.POSTER' => ($r['cm_user_id'])?($func->get_data('user',$r['cm_user_id'])):$lang['guest'],
				'cm.AVATAR'	=> $avatar_include,
				'cm.CONTENT' =>	$cm_content,
				'cm.DATE' => $func->makedatetime($r['cm_date']),
				'cm.POSTER_URL' => ($r['cm_user_id'])?$func->change_link('user',$r['cm_user_id'],$func->title_2_ascii($func->get_data("user",$r['cm_user_id'],"u_name"),true)):"#GUEST",
				'cm.ID'			=> $r['cm_id'],
				'multi.ID'		=> $multi_id,
				'media.ID'		=> $media_id,
			)
		);
	$i++;
	}
	$ro = $tpl->assign_vars($ro,array(
			'cm.TOTAL'     => $cm_total,
			'cm.PAGES'	   => $view_pages,
		)
	);

	$html = $tpl->assign_blocks_content($ro,array(
			'comment_block'	=>	$html
		)
	);
	$level_admin=$func->check_level($_SESSION['ses_user_id']);
	if(!$logged_in)
	$html=$tpl->unset_block($html,array('del'));
	else{
		if($level_admin!=1)
		$html=$tpl->unset_block($html,array('del'));
	}
	return $tpl->parse_tpl($html);
}

function box_login() {
	global $DB,$conf,$tpl,$func,$logged_in;
	$login_array=	array(	
			'login.FORGOTPASS' => $func->change_link("user","forgot","forgot_pass"),
			'login.REGISTER' => $func->change_link("register",'',"new_register"),
			'login.LOGIN'	=> $func->change_link("user_log","login"));	
	if($logged_in){
		$block="box_logged";
		$qr=$DB->query("SELECT * FROM {$conf['prefix']}user WHERE u_id='".$_SESSION['ses_user_id']."'");
		$u=$DB->fetch_row($qr);
		$logged_array=array(
			'logged.USERNAME'		=> $func->unhtmlchars(@$u['u_name']),
			'logged.AVATAR'		=> $func->get_img("avatar",@$_SESSION['ses_user_id']),
			'logged.LOGOUT'	=> $func->change_link("user_log","logout"),
			'logged.SMS'	=> $func->change_link("sms"),
			'logged.MY_HOME'	=> $func->change_link("user",$_SESSION['ses_user_id'],"my_home"),
			'logged.CHANGE_INFO' => $func->change_link("user","change_info","change_info"));
		array_push($login_array,$logged_array);
	}
	else $block="box_login";
	$ro=$tpl->get_tpl($block);
	$ro = $tpl->assign_vars($ro,$login_array);
	return $ro;
}
function box_playlist() {
	global $DB,$conf,$tpl,$func,$logged_in,$lang;
	$ro=$tpl->get_tpl("box_playlist");
	$html='';	
	if($logged_in){
	$t['row']=$tpl->get_block_from_str($ro,"playlist.row",1);
	$t['song_row']=$tpl->get_block_from_str($ro,"song.row",1);
		$qr=$DB->query("SELECT * FROM {$conf['prefix']}playlist WHERE pl_userid='".$_SESSION['ses_user_id']."'");
		$ro=$tpl->unset_block($ro,array('guest_block'));
		
		if($DB->num_rows($qr)){
			while($r=$DB->fetch_row($qr)){
				$html.=$tpl->assign_vars($t['row'],array(
						'pl.ID'	=> $r['pl_id'],
						'pl.FULLNAME' => $func->unhtmlchars($r['pl_name']),
						'pl.NAME' => $func->getwords($r['pl_name'],5),
						'pl.URL'	=> $func->change_link("playlist",$r['pl_id'],$func->title_2_ascii($r['pl_name'],true)),
					)
				);
				$qpl=$DB->query("SELECT pl_contents FROM {$conf['prefix']}playlist WHERE pl_id={$r['pl_id']}");
				$fet=$DB->fetch_row($qpl);
				$pl_contents=$fet['pl_contents'];
				$jqr=$DB->query("SELECT * FROM {$conf['prefix']}media WHERE m_id IN ({$pl_contents})");
				if($DB->num_rows($jqr)){
					while($j=$DB->fetch_row($jqr)){
						$media_title=$func->unhtmlchars($j['m_title']);
						$html.=$tpl->assign_vars($t['song_row'],array(
								'song.ID'	=> $j['m_id'],
								'song.FULLTITLE' => $media_title,
								'song.TITLE' => $func->getwords($media_title,5),
								'song.URL'	=> $func->change_link("song",$j['m_id'],$func->title_2_ascii($j['m_title_ascii'])),
								'song.PL_ID'	=> $r['pl_id'],
							)
						);
					}
				} 
			}

		}else  $html=$lang['empty_playlist'];
		$ro=$tpl->assign_vars($ro,array(
					'pl.URL_ADD_PL' => $func->change_link("add_playlist","","add_playlist"),
				)
			);
	}
	else{
		$htm='';
		$t['song_row']=$tpl->get_block_from_str($ro,"guest_song.row",1);
		$ro=$tpl->unset_block($ro,array('user_block'));
			$pl_contents=(!empty($_SESSION['ses_playlist']))?$_SESSION['ses_playlist']:"";
			if($pl_contents){
			$jqr=$DB->query("SELECT * FROM {$conf['prefix']}media WHERE m_id IN ({$pl_contents})");			
				while($j=$DB->fetch_row($jqr)){
					$media_title=$func->unhtmlchars($j['m_title']);
					$htm.=$tpl->assign_vars($t['song_row'],array(
						'song.ID'	=> $j['m_id'],
						'song.FULLTITLE' => $media_title,
						'song.TITLE' => $func->getwords($media_title,5),
						'song.URL'	=> $func->change_link("song",$j['m_id'],$func->title_2_ascii($j['m_title_ascii'])),
						)
					);
				}
			}
			else $htm=$lang['empty_playlist'];
			$ro=$tpl->assign_vars($ro,array(
					'pl.PLAY_URL' => $func->change_link("playlist","guest","guest"),
				)
			);
	}
	$ro = $tpl->assign_blocks_content($ro,array(
			'playlist'	=>	$html,
			'guest_playlist'	=> $htm,

		)
	);
	return $tpl->parse_tpl($ro);
}

function box_user_online() {
	global $DB, $conf,$func,$tpl,$lang;
	$html = $tpl->get_tpl("box_user_online");
	$c_user_online = $tpl->get_block_from_str($html,'user_online');
	$c = $tpl->auto_get_block($c_user_online);
	$timeout = 60*30;
	$total = 0;	$member = 0; $guest = 0;
	$current_time = NOW;
	$time_exit = $current_time - $timeout;
	$DB->query("DELETE FROM ".$conf['prefix']."online WHERE timestamp < ".$time_exit);
	$guests = $DB->num_rows($DB->query("SELECT DISTINCT sid FROM ".$conf['prefix']."online WHERE sid != ''"));
	
	$member_sql = $DB->query("SELECT u_id, u_name, u_level FROM ".$conf['prefix']."user WHERE u_online = 1 AND u_timeout > ".$time_exit." ORDER BY u_timeout DESC");
	$members = $DB->num_rows($member_sql);
	$total = $guests + $members;
	
	while($r = $DB->fetch_row($member_sql)){
		$id = $r['u_id'];
		$level = $r['u_level'];
		$name = $r['u_name'];
		if ($id == $_SESSION['ses_user_id']) $s = $c['this'];
		elseif ($level == 3) $s = $c['member'];
		elseif ($level == 2) $s = $c['mod'];
		elseif ($level == 1) $s = $c['admin'];
		$online_list .= $tpl->assign_vars($s,
			array(
				'user.ID'	=>	$id,
				'user.NAME'	=>	$name,
				'user.URL'	=>	$func->change_link('user',$id,$func->title_2_ascii($name,true)),
			)
		);
	}
	$html = $tpl->assign_vars($html,
			array(
				'user.TOTAL'		=>	$total,
				'user.GUESTS'	=>	$guests,
				'user.MEMBERS'	=>	$members,
			)
		);
	
	$html = $tpl->assign_blocks_content($html,
		array(
			'user_online'	=>	$online_list,
		)
	);
	return $html;
}

function box_relate_song($album_id,$song_id){
	global $DB,$func,$conf,$tpl,$lang;
	$sql=$DB->query("SELECT * FROM {$conf['prefix']}media WHERE m_multimedia={$album_id} AND m_id<>{$song_id} ORDER BY m_id DESC LIMIT 7");
	$tt=$DB->num_rows($sql);
	if ($tt) {
		$ro = $tpl->get_tpl('list_media');
		$t['row'] = $tpl->get_block_from_str($ro,'list_media.row',1);
		
		$html = '';
		$list_title=$lang['relate_song_in_album'];

		while ($song = $DB->fetch_row($sql)) {
			static $i = 0;	
			$singer_name=$func->get_data('singer',$song['m_singer']);
			$html .= $tpl->assign_vars($t['row'],
				array(
				'song.TITLE'	=> $func->unhtmlchars($song['m_title']),
				'song.TYPE'		=> "<img src='{TPL_LINK}/".$func->get_data('player',$song['m_type'])."' border=0 >",
				'song.ID'		=> $song['m_id'],	
				'song.VIEWED'	=> $song['m_viewed'],
				'song.DOWNLOADED' => $song['m_downloaded'],
				'song.SINGER_NAME' => $singer_name,
				'song.SINGER_URL' => $func->change_link('singer',$song['m_singer'],$func->title_2_ascii($singer_name,true)),
				'song.URL'		=> $func->change_link('song',$song['m_id'],$func->title_2_ascii($song['m_title_ascii'])),
				'song.ADD_PL'	=> $logged_in?"select_playlist({$song['m_id']});return false;":"guest_playlist({$song['m_id']});return false;",
				)
			);
			$i++;
		}
			$ro=$tpl->unset_block($ro,array('album_in_cat'));
			$album_ascii=$func->get_data("multimedia",$album_id,"multi_name_ascii");
		$ro = $tpl->assign_vars($ro,
			array(
				'list.TITLE' => $list_title,
				'list.TOTAL'	=> "",
				'list.PAGES' => "<center><a href='".$func->change_link("album",$album_id,$func->title_2_ascii($album_ascii))."'><font color=blue><b>{$lang['view_more']}</b></font></a></center>",
			)
		);
		
		$ro = $tpl->assign_blocks_content($ro,array(
				'list_media'	=>	$html,
			)
		);
		
	
	}
	else $ro=$lang['no_data'];
	return $ro;


}

function box_link_img() {
	global $DB, $tpl,$conf,$func;
	$ads_img= $tpl->get_tpl('box_link_img');
	$sql_img=$DB->query("SELECT * FROM {$conf['prefix']}ads WHERE ads_img<>'' ORDER BY ads_id ASC");
	$html_img='';
	if($DB->num_rows($sql_img)){
		$t['row'] = $tpl->get_block_from_str($ads_img,'box_link_img.row',1);
		while($link_img=$DB->fetch_row($sql_img)){
			$html_img.= $tpl->assign_vars($t['row'],
				array(
					'link.IMG'	=>	$link_img['ads_img'],
					'link.URL'	=> $link_img['ads_url'],
					'link.WEB'	=> $link_img['ads_web'],
				)
			);
		}
			$ads_img = $tpl->assign_blocks_content($ads_img,
				array(
					'box_link_img'		=> $html_img,
				)
			);
	}
	return $ads_img;
}

function box_link_text() {
	global $DB, $tpl,$conf,$func;
	$ads= $tpl->get_tpl('box_link_text');
	$sql=$DB->query("SELECT * FROM {$conf['prefix']}ads WHERE ads_img='' ORDER BY ads_id ASC");
	$html='';
	if($DB->num_rows($sql)){
		$t['row'] = $tpl->get_block_from_str($ads,'box_link_text.row',1);
		while($link=$DB->fetch_row($sql)){
			$html.= $tpl->assign_vars($t['row'],
				array(
					'link.URL'	=> $link['ads_url'],
					'link.WEB'	=> $link['ads_web'],
				)
			);
		}
			$ads = $tpl->assign_blocks_content($ads,
				array(
					'box_link_text'		=> $html,
				)
			);
	}
	return $ads;
}
function box_focus_multi($type_multi,$type) {
	global $DB, $tpl,$conf,$func;
	
	if($type_multi=='album'){
		 $ju=1;
		  if($type=='vn') $ro_type=1;
		  elseif($type=='fr') $ro_type=2;
		}
	elseif($type_multi=='movie'){
		$ju=2;
	  	  if($type=='hot') $ro_type=3;
		  elseif($type=='cine') $ro_type=4;
	}
	$sql=$DB->query("select F.*,multi_name,multi_name_ascii from {$conf['prefix']}focus F left join {$conf['prefix']}multimedia MU on F.focus_multi=MU.multi_id where multi_type={$ju} and multi_id in(select focus_multi from {$conf['prefix']}focus where focus_multi<>'' and focus_type={$ro_type}) order by focus_id desc limit 5");
	$html_img='';
	$html_desc='';
	$html_thumb='';
	if($DB->num_rows($sql)){
	$slide= $tpl->get_tpl('slide_focus');
		$t['row_img'] = $tpl->get_block_from_str($slide,'slide_img.row',1);
		$t['row_desc'] = $tpl->get_block_from_str($slide,'slide_desc.row',1);
		$t['row_thumb'] = $tpl->get_block_from_str($slide,'slide_thumb.row',1);
		while($s=$DB->fetch_row($sql)){
			$link=$func->change_link($type_multi,$s['focus_multi'],$func->title_2_ascii($s['multi_name_ascii']));
			$html_img.= $tpl->assign_vars($t['row_img'],
				array(
					'slide.URL'	=> $link,
					'slide.IMG'	=> $conf['web_url']."/".$conf['focus_img']."/".$s['focus_img'],
				)
			);
			$html_desc.= $tpl->assign_vars($t['row_desc'],
				array(
					'slide.TITLE'	=> $func->unhtmlchars($s['multi_name']),
					'slide.URL'	=> $link,
					'slide.DESC'	=> $func->unhtmlchars($s['focus_desc']),
				)
			);
			$html_thumb.= $tpl->assign_vars($t['row_thumb'],
				array(
					'slide.THUMB'	=> $conf['web_url']."/".$conf['focus_thumb']."/".$s['focus_img'],
				)
			);
		}
			$slide = $tpl->assign_blocks_content($slide,
				array(
					'slide_img'		=> $html_img,
					'slide_desc'		=> $html_desc,
					'slide_thumb'		=> $html_thumb,
				)
			);
	}
	return $slide;
}
function box_television() {
	global $DB, $tpl,$conf,$func;
	$html = $tpl->get_tpl("box_television");
	$html = $tpl->assign_vars($html,
		array(
			'box_tivi.URL'	=>	$func->change_link("tivi","","television"),
		)
	);
	return $html;
}

function box_video() {
	global $DB, $tpl,$conf,$func,$lang;
	$html = $tpl->get_tpl("box_video");
	$html = $tpl->assign_vars($html,
		array(
			'box_video.URL'	=>	$func->change_link("video","",$func->title_2_ascii($lang['funny_clip'],true)),
		)
	);
	return $html;
}

function get_show_box($temp){
	global $tpl;
	$html = $tpl->get_tpl($temp);
	return $html;
}
?>